package com.gccloud.design.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.OptButtonBranchDao;
import com.gccloud.design.dto.OptButtonBranchDto;
import com.gccloud.design.entity.OptButtonBranchEntity;
import com.gccloud.design.service.OptBtnBranchService;
import com.gccloud.design.vo.OptButtonBranchVo;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.feign.api.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ma.xiulan
 * @date 2021/05/24/ 11:01
 */
@Service
public class OptBtnBranchServiceImpl extends ServiceImpl<OptButtonBranchDao, OptButtonBranchEntity> implements OptBtnBranchService {
    @Resource
    private OptButtonBranchDao optBtnBranchDao;

    @Resource
    private ISysUserService iSysUserService;

    /**
     * 流程操作方法
     */
    @Override
    public Map<String, Object> getOptBtnBranch(Integer businessTypeId, String searchCon, SearchDTO searchDTO) {
        Page<OptButtonBranchVo> page = new Page<>(searchDTO.getCurrent(), searchDTO.getSize());
        List<OptButtonBranchVo> list = optBtnBranchDao.getOptBtnBranch(businessTypeId, searchCon, page);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public Boolean saveOptBtnBranch(OptButtonBranchDto optButtonBranchDto) {
        OptButtonBranchEntity optButtonBranchEntity = BeanConvertUtils.convert(optButtonBranchDto, OptButtonBranchEntity.class);
        optButtonBranchEntity.setCreateUserId(iSysUserService.getCurrentUser().getData().getId());
        optButtonBranchEntity.setCreateUserName(iSysUserService.getCurrentUser().getData().getName());
        int result = optBtnBranchDao.saveOptBtnBranch(optButtonBranchEntity);
        return result > 0;
    }

    @Override
    public Boolean saveUpdateOptBtnBranch(OptButtonBranchDto optButtonBranchDto) {
        OptButtonBranchEntity optButtonBranchEntity = BeanConvertUtils.convert(optButtonBranchDto, OptButtonBranchEntity.class);
        optButtonBranchEntity.setUpdateUser(iSysUserService.getCurrentUser().getData().getId());
        int result = optBtnBranchDao.saveUpdateOptBtnBranch(optButtonBranchEntity);
        return result > 0;
    }

    @Override
    public Boolean deleteOptBtnBranch(Integer optBranchId) {
        int count = optBtnBranchDao.isRelied(optBranchId);
        if (count > 0) {
            return false;
        } else {
            optBtnBranchDao.deleteOptBtnBranch(optBranchId);
            return true;
        }
    }


}
